à Embedded Programming

Reading the data sheet


This week, we have to program the hello board we did on the electronic design week, for the first time I will be writing software for hardware. I started by reading the datasheet of my attiny44.



I read the datasheet of the attinny44, and also informed myself on the purpose and caractheristic in microcontroller in general, and learn a few things. I did not understood everything, but it was very nice to learn more about the pinout of the chip and what everything is supposed to do.



I also learn that a microcontroller has a cpu with a fixed amount of RAM and ROM, both microcontroller and microprocessor serve the same application, wich is to run real time application program. The microprocessor differs from the microcontroller by the fact that it only contain a CPU, the other peripheral need to be added externally to make them functional. (ex: a computer contain a processor and is wired onto the Motherboard, externally we fixed onto the motherboard the RAM, the GPU and others things.)



Thats why a microcontroller is the best choice to run simple program, its a real compact version of a full functional computer, it's cheap and it only need itself to be fully functionnal.



Here we can see the diferent component that we found inside the chip. I don't know whats everything is supposed to do but we can see the 8-bit databus, an oscillator, some RAM, some registers (32) wich are all connect to the Arithmetic Logic Unit (ALU) that allow two independent register to be accessed in one instruction executed in one clock cycle, this architecture is more code efficient and achieve throughtputs 10 times faster tant a conventional microcontroller.

Also, I saw that the Attiny 45 as an integrated 8 MHz oscillator that makes the 20 MHz crystal on our fabisp not necessary. I also saw with my instructor Raphael that the arduino code does not corespond with the pinout from the Attiny 45, I then find this picture that illustrate how you should use the pinout of an attitiny on an arduino.



We can also see on the pinout of the attiny44 that some pins are able to do Pulse With Modulation (PWM) I wanted to know what exactly was the functionning of a PWM process, I already knew it was some kind of digital signal simulating analog but I wanted to know more.

I went and looked into this Sparkfun tutorial about PWM and learn that PWM is effectively a type of digital signal that can vary how much time the signal is set to high. This give you more control on the signal than a simple and common digital signal.

Exemple, you can dim and change color of an RGB LED or the position of a servo using a Pulse With Modulation digital signal.

Programming my Hello Board


I use the Arduino IDE to program my helloboard, the interface of this IDE is really "noob" friendly and the community behind it is juste immense. So for me, a beginner who is on a deadline, it was a no-brainer to go with this IDE. ("The arduino IDE is a code editor with features such as text cutting and pasting, searching and replacing text, automatic indenting, brace matching, and syntax highlighting, and provides simple one-click mechanisms to compile and upload programs to an Arduino board.") As explained on the "wiki".

It support C and C++ language and using special rules of code structuring. It also provide a software library coming from the Wiring project. It is very similar from the software processing but is made for programming hardware such as sensor, microcontroller ans else while processing is made to create interactive interface and GUI.

All I wanted to do with my helloboard is a simple program that blink my led when I press the pushbutton. So looking at my board design I already know wich pin will be used in my code.

We know that I will use my physical pin 5 (pin 8 on the arduino IDE) for my LED, and the physical pin 10 (pin 3 on the arduino IDE) for my Button.

Knowing that we can start our code by defining our pin using this line of code.

const int ledPin = 8 ;
const int buttonPin = 3 ;


The const prefix mean that the variable won't change during the time of the code running, the variables are "constant"
The "int" prefix mean that the variable is an integer, which is a full number 0,1,2,3 are all integer, while 2.1 would be a float number.

Next we will prepare the variable to read and check the button and led status.
int buttonState = 0 ;
bool led_on;


buttonState = 0 just set the fact that we will later use a variable called button state, as you can see this variable ain't constant, because the state of the button will change eache time we will press it.
A boolean is a way to check if a fact is true or false at any time in our code. In our case, the led is ON : true or false?

Now that our variable are set, we can start writing our actual code, we first need to setup the code that run only once at the begining of the reading of our code, we called that part the setup and once it run, it won't be read again until we reset our code.

pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);


This is pretty straightforward, we set the ledpin as an Output pin and the buttonpin as an Input.

Now we have to create a Loop, a loop is the part of the code that will be read over and over again, it is the actual "action" that the code will do.

if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}


Here we are, now we have to test the code, normally everything should work.

To insert the code we first need to set up our Arduino IDE to program our attiny44. So we will do just like we did with our ISP.

Into the board tab we will go and select our Attiny44, make sure we use our internal 8mhZ oscillator and set the programmer as a tinyusb since we will be using our FabISP to program the board.

Next we will plug in our FabISP to our HelloBoard :



I upload the code and guess what?
It worked!!



Here is the final code:

  // Set Pins
const int ledPin =  8 ;
const int buttonPin = 3 ;


int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } 
  
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
} 


« Go back to assigments page